import java.awt.Graphics;
import java.awt.Panel;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;
import java.util.ArrayList;
import java.util.Iterator;
import javax.swing.JPanel;

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
/**
 *
 * @author 212010509
 */
public class DrawPanel extends JPanel {

    private boolean geoPressed(int[] point) {
        Iterator<GeoDrawable> it = m_drawables.iterator();
        boolean found = false;
        GeoDrawable gd = null;
        while (it.hasNext() && !found) {
            gd = it.next();
            Handle h = gd.handleContains(point);
            if (h != null) {
                found = true;

            }
        }
        if (found) {
            if (CurrentSelected != null) {
                CurrentSelected.setSelected(false);
            }
            gd.setSelected(true);
            CurrentSelected = gd;
            repaint();
        }
        return found;
    }

    private void pressed(int[] tmpCoord) {
        Iterator<GeoDrawable> gi = this.m_drawables.iterator();
        while (gi.hasNext()) {
            GeoDrawable gd = gi.next();
            Handle h  = gd.handleContains(tmpCoord);
            if (h != null) {
                CurrentSelected = gd;
                gd.setSelected(true);
                gd.pressed(tmpCoord);
            }
        }
    }

    class DrawMotionEventHandler implements MouseMotionListener {

        int[] tmpCoord;
        DrawPanel m_panel = null;

        public DrawMotionEventHandler(DrawPanel panel) {
            m_panel = panel;
            tmpCoord = new int[2];
        }

        @Override
        public void mouseDragged(MouseEvent e) {

            if (m_panel.CurrentSelected != null) {
                tmpCoord[0] = e.getX();
                tmpCoord[1] = e.getY();
                m_panel.CurrentSelected.drag(tmpCoord);
            }
            repaint();
        }

        @Override
        public void mouseMoved(MouseEvent e) {
            if (m_panel.CurrentSelected != null) {
                tmpCoord[0] = e.getX();
                tmpCoord[1] = e.getY();
                m_panel.CurrentSelected.drag(tmpCoord);
            }
            repaint();
        }

    };

    class DrawMouseEventHandler implements MouseListener {

        int[] tmpCoord;
        DrawPanel m_panel;

        public DrawMouseEventHandler(DrawPanel panel) {
            m_panel = panel;
            tmpCoord = new int[2];
        }

        @Override
        public void mouseClicked(MouseEvent e) {
            ;
        }

        @Override
        public void mousePressed(MouseEvent e) {
            tmpCoord[0] = e.getX();
            tmpCoord[1] = e.getY();
            if (m_panel.CurrentSelected != null) {
                boolean discovered = m_panel.CurrentSelected.pressed(tmpCoord);
                if (!discovered) {

                    System.out.println("Unselected all.");
                    if (CurrentSelected != null) {
                        CurrentSelected.setSelected(false);
                    }
                    CurrentSelected = null;
                }
            } else {
                m_panel.pressed(tmpCoord);
                

            }
            repaint();
        }

        @Override
        public void mouseReleased(MouseEvent e) {
            tmpCoord[0] = e.getX();
            tmpCoord[1] = e.getY();
            if (m_panel.CurrentSelected != null) {
                m_panel.CurrentSelected.released(tmpCoord);
            }
            repaint();
        }

        @Override
        public void mouseEntered(MouseEvent e) {
            ;
        }

        @Override
        public void mouseExited(MouseEvent e) {
            ;
        }

    };
    protected DrawMouseEventHandler m_handler;
    protected DrawMotionEventHandler m_motionHandler;

    public DrawPanel() {
        super();
        m_drawables = new ArrayList<>();
        CurrentSelected = null;

        m_handler = new DrawMouseEventHandler(this);
        m_motionHandler = new DrawMotionEventHandler(this);
        addMouseListener(m_handler);
        addMouseMotionListener(m_motionHandler);
    }

    public void addDrawable(GeoDrawable gd) {
        m_drawables.add(gd);
        CurrentSelected = gd;
        gd.setSelected(true);
        repaint();
    }

    ArrayList<GeoDrawable> m_drawables;
    GeoDrawable CurrentSelected;

    public void paintComponent(Graphics g) {
        super.paintComponent(g);
        Iterator<GeoDrawable> gi = m_drawables.iterator();
        while (gi.hasNext()) {
            GeoDrawable gd = gi.next();
            gd.draw(g);
        }
    }
}
